import LonginViews from '../views/LonginViews.vue' 
import chatViews from '../views/chatViews.vue'  
import HomeViews from '../views/HomeViews.vue' 
import navViews from '../views/navViews.vue' 
import mmViews from '../views/mmViews.vue'
import taskViews from '../views/taskViews.vue'
import mapViews from '../views/mapViews.vue'
import alarmViews from '../views/alarmViews.vue'
import { createRouter, createWebHistory } from 'vue-router'



const routes = [
  {
    path: '/',  
    redirect: '/LonginViews'
  },
  {
    path: '/LonginViews',  
    name: 'LonginViews',
    component: LonginViews 
  },
  {
    path: '/chatViews',
    name: 'chatViews',     
    component: chatViews  
  },
  {
    path: '/HomeViews',
    name: 'HomeViews',     
    component: HomeViews 
  },
  {
    path: '/navViews',
    name: 'navViews',     
    component: navViews 
  },
    {
    path: '/mmViews',
    name: 'mmViews',     
    component: mmViews 
  },
  {
    path: '/taskViews',
    name: 'taskViews',     
    component: taskViews 
  },
  {
    path: '/mapViews',
    name: 'mapViews',     
    component: mapViews 
  },
  {
    path: '/alarmViews',
    name: 'alarmViews',     
    component: alarmViews 
  },
 
]

const router = createRouter({
  history: createWebHistory('/'), 
  routes
})
// router.beforeEach((to, from, next) => {
//   // 判断是否已登陆，sessionStorage.getItem('showLogin') === '0'代表了已登录
//   // 已登录：isAuthenticated==true，未登录：isAuthenticated==false
//   const isAuthenticated = sessionStorage.getItem('showLogin') === '0'
//   // 判断目标路由是否为 /LoginDouBao
//   if (to.path === '/LonginViews') {
//     next() // 直接放行
//   } else if (!isAuthenticated) {
//     next({ path: '/LonginViews' })
//   } else {
//     next()
//   }
// })

// router/index.js

router.beforeEach((to, from, next) => {
  // 通过sessionStorage判断用户是否登录
  const isAuthenticated = sessionStorage.getItem('sessionId') !== null
  
  // 登录页直接放行
  if (to.path === '/LonginViews') {
    next()
  } 
  // 非登录页需要验证登录状态
  else if (!isAuthenticated) {
    next({ path: '/LonginViews' })
  } 
  // 已登录状态正常跳转
  else {
    next()
  }
})

export default router

